package com.director.example.action;

import com.director.core.ProviderType;
import com.director.core.annotation.DirectMethod;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: Simone Ricciardi
 * Date: 1-giu-2010
 * Time: 18.04.28
 */
@ParentPackage("struts-director")
public class TestAction extends ActionSupport {

   @Action("multiply")
   @DirectMethod
   public String multiply(int a) {
      return "Result : " + (a * 8);
   }

   @Action("doEcho")
   @DirectMethod
   public String echo(String message) {
      return message;
   }

   @Action("poll")
   @DirectMethod(providerType = ProviderType.POLLING)
   public void poll() {
      this.addActionMessage("Successfully polled at: " + new SimpleDateFormat("H:m:s").format(new Date()));
   }

   @Action("getTree")
   @DirectMethod
   public List<Map> getTree(String id) {
      List<Map> out = new ArrayList<Map>();
      if("root".equals(id)) {
         for(int i = 1; i <= 5; ++i) {
            Map<String, Object> node = new HashMap<String, Object>();
            node.put("id", "n" + i);
            node.put("text", "Node " + i);
            node.put("leaf", false);
            out.add(node);
         }
      } else if(id.length() == 2) {
         int num = Integer.parseInt(id.substring(1));
         for(int i = 1; i <= 5; ++i) {
            Map<String, Object> node = new HashMap<String, Object>();
            node.put("id", id + i);
            node.put("text", "Node " + num + '.' + i);
            node.put("leaf", true);
            out.add(node);
         }
      }
      return out;
   }
}
